09. Exercise: DAO - SleepDatabaseDao
L6 14 DAO SleepDatabaseDao Code SC
Update:
All the functions declared inside theSleepDatabaseDaointerface should be declared assuspend, except the one which returns a LiveData. The feature of a suspend function is that it can suspend the execution (pause and resume at a later time) of a coroutine.
Now it’s your turn to complete this exercise yourself.
In this step, you'll create queries for inserting, requesting,, and updating sleep data.
- Create an interface
SleepDatabaseDaoand annotate it with@Dao:
@Dao
interface SleepDatabaseDao {}
Add an
@Insert annotation, and aninsert()function that takes oneSleepNight.That's it.
Roomwill generate all the necessary code to insert the passed-inSleepNightinto the database. Note that you can call the function anything you want:
@Insert
suspend fun insert(night: SleepNight)
- In the same way, add an
@Updateannotation with anupdate()function for oneSleepNight:
@Update
suspend fun update(night: SleepNight)
- Add a
@Queryannotation with a functionget()that takes aLongkey argument and returns a nullableSleepNight:
@Query
suspend fun get(key: Long): SleepNight?
Add a parameter to
@Query.Make it a
Stringthat is a SQLite query that selects all columns from thedaily_sleep_quality_table WHEREthenightIdmatches the:keyargument.
@Query("SELECT * from daily_sleep_quality_table WHERE nightId = :key")
suspend fun get(key: Long): SleepNight?
- Add another
@Querywith aclear()function and a SQLite query to delete everything from thedaily_sleep_quality_table:
@Query("DELETE FROM daily_sleep_quality_table")
suspend fun clear()
**Add a
@QuerytogetAllNights(). **The SQLite query should return all columns from the
daily_sleep_quality_table, ordered in descending order. LetgetAllNights()return a list ofSleepNightasLiveData.Roomkeeps thisLiveDataupdated for us, and we don't have to specify an observer for it.
@Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC")
fun getAllNights(): LiveData<List<SleepNight>>
Add a
@QuerytogetTonight(). Make the returnedSleepNightnullable, so that it can handle if the table is empty.You get tonight by writing a SQLite query that returns the first element of a list of results ordered by nightId in descending order. Use
LIMIT 1to return only one element.
@Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC LIMIT 1")
suspend fun getTonight(): SleepNight?
- Run your app to make sure it has no errors.
If you want to start at this step, you can download this exercise from: Step.02-Exercise-Create-SleepDatabaseDao.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here: Step.02-Solution-Create-SleepDatabaseDao, or using this git diff.
Task Description:
Complete the following tasks to create a SleepDatabase DAO.
Task Feedback:
Great, you have defined how your app is going to interact with the database!
Reference Documentation